var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import { $wuxDialog } from '../../components/wux'

var wxCharts = require('../../utils/chart');
var lineChart = null;

var app = getApp();
var that;


Page({
  data: {
    user: {}
    ,isload:0
    ,sysset:{}
    ,istcVisible: false
    ,istcVisible2: false
    ,opop:0
    ,stop: 1000
    ,sleft: 1000

    ,stepall:0
    ,step:0
    ,date:null
    ,isgetkb:false
    ,isgl:false
    ,isadmin:false
    ,isload:true
    ,kbobj:{kb:0,kbyd:0,kbxf:0}
    ,kblimit:0

    ,xh_cake:0
    ,xh_cal:0
    ,encryptedData:null
    ,windowWidth:300
    ,type:0
  },
  onShareAppMessage: function () {
      return app.getShare();
  },

  onPullDownRefresh:function(){
    that.loadData();
  },
  onShow:function(){
   
  },
  onReady:function(){
    that.data.isload=0;
    
  },
  closeTc:function(){
    this.setData({
      istcVisible: false,
    });
  },
  closeTc2:function(){
    this.setData({
      istcVisible2: false,
    });
  },
  onPullDownRefresh:function(){
    that.loadData();
  },
  onLoad: function (options) {
    that = this;
    if( !util.judge.isNull( options.type) ){
      that.setData({
        type :options.type
      });
    }

    that.loadData();
  },
  loadData:function(){
    
    util.showLoading();
    app.getUserInfo()
    .then(res => {
      var bl = false;
      var bl2 =false;
      if(res.isgl==1){
        bl =true;
      }
      if(res.isadmin ==1){
        var bl2 = true;

      }

      that.setData({
        user:res
        ,isgl:bl
        ,isadmin:bl2
      })
      //获取用户的个人信息 包括读取接口
      var url = config.url_user_get;
      var params = {
       id:app.globalData.user.uid
      }
      return wxRequest.postRequest(url, params);

    })
    .then(res=>{
        if(res == 21){
            throw util.stop();
        }

      res.dkuser = util.toobj(res.dkuser);
      console.log(res);
      that.setData({
          'kbobj.kb':res.dkuser.kbnow
      });
      if(res.dkuser && res.dkuser.success ){
         that.setData({
          'kbobj.kbyd':res.user.ydkb,
          'kbobj.kbxf':res.dkuser.kball
        });
      }
      var url = config.url_get_sysset;
      var params = {}
      return wxRequest.postRequest(url, params);
    })
    .then(res=>{

      // res.user 用户的信息  res.dkuser  dk用户的信息
      that.setData({
          sysset:res,
          stepall:res.kblimit
      });
      // util.alert(res.kblimit);
      console.log(res);

      


      var dd = new Date();
      var val = util.date2str(dd,"yyyy-MM-dd");

      var url = config.url_user_list_step;
      var params = {
        // uid:111111111,
        pageIndex: 1,
        pageSize:7,
        date:val,
        code:""
      }

      return wxRequest.postRequest(url, params)
    
    })
    .then(res=>{
      
      var windowWidth = 365;
      try {
          var tsi = wx.getSystemInfoSync();
          windowWidth = tsi.windowWidth - 10;
          that.setData({
            windowWidth:windowWidth
          });
      } catch (e) {
          console.error('getSystemInfoSync failed!');
      }
      
      var dc = [];
      var dd = new Date();
      for(var i=6;i>=0;i--){
        var tdd = dd.valueOf();
        tdd =  tdd - i * 24 * 60 * 60 * 1000;
        tdd = new Date(tdd)
        var val = util.date2str(tdd,"MMdd");
        dc.push(val);
      }

      var sd=[0,0,0,0,0,0,0];
      for(var j in res){
          var bd = util.date2str(util.str2date(res[j].udate),"MMdd");
          for(var i = 0;i<7;i++){
              if(dc[i]== bd ){
                sd[i] = res[j].step;
                break;
              }
          }
      }

      var  name ="每日步数";
      if(that.data.type == 1){
         
      }else if(that.data.type == 2){
          name ="消耗卡路里(cal)";
          var cal =0;
          for(var i in sd){
              var step = sd[i];
              if(step>0){
                  if(that.data.sysset.step_cal){
                      cal = step / that.data.sysset.step_cal;
                  }
                  sd[i] = cal.toFixed(1);
              }
          }

      }
      else if(that.data.type == 3){
          name ="消耗蛋糕(块)";
          for(var i in sd){
              var step = sd[i];
              var cake =0;
              if(step>0){
                  if(that.data.sysset.step_cake){
                      cake = step / that.data.sysset.step_cake;
                  }
                  sd[i] = cake.toFixed(1);
              }
          }
      }


      new wxCharts({
          canvasId: 'lineCanvas',
          type: 'line',
          categories: dc,
          series: [{
              name: name,
              data: sd,
              format: function (val) {
                  return val;
              }
          }],
          xAxis: {
            gridColor:"#ffffff",
            fontColor:"#ffffff",
            fontSize:18
          },
          yAxis: {
              title: '',
              format: function (val) {
                  return val.toFixed(0);
              },
              // min: 0,
              gridColor:"#ffffff",
              fontColor:"#ffffff",
              fontSize:18
          },
          width: windowWidth,
            height: 200,
            dataLabel: true,
            dataPointShape: false,
            extra: {
                lineStyle: 'curve',
                legendTextColor:"#ffffff"
            },
            fontSize:16
      });
    })
    .catch(res => {
      util.alert_catch(res);
    })
    .finally(function (res) {
      util.hideLoading();
      wx.stopPullDownRefresh();
    });
 
  }
  

})